package com.nanyang.tms.controller.cost;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.cost.CostQueryRequest;
import com.nanyang.tms.api.dto.request.cost.CostUpdateStateRequest;
import com.nanyang.tms.api.dto.request.cost.add.CostBaseAddRequest;
import com.nanyang.tms.api.dto.request.cost.update.CostBaseUpdateRequest;
import com.nanyang.tms.api.dto.response.cost.CostBaseResponse;
import com.nanyang.tms.api.dto.response.cost.CostPageResponse;
import com.nanyang.tms.api.service.cost.CostRpcService;

@RestController
@RequestMapping("/cost")
public class CostController {

    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private CostRpcService costRpcService;

    /**
     * 列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<CostPageResponse>> listCost(@RequestBody CostQueryRequest param) {
        ApiResult<PageBean<CostPageResponse>> apiResult = costRpcService.listCost(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> saveCost(@BindParam @RequestBody @Valid CostBaseAddRequest param) {
        ApiResult<Boolean> apiResult = costRpcService.saveCost(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 修改
     * @param param
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> updateCost(@BindParam @RequestBody @Valid CostBaseUpdateRequest param) {
        ApiResult<Boolean> apiResult = costRpcService.updateCost(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 禁用
     * @param param
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> disableState(@BindParam @RequestBody @Valid CostUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = costRpcService.disableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 启用
     * @param param
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> enableState(@BindParam @RequestBody @Valid CostUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = costRpcService.enableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listSupplier</p>
     * <p>Description:查询根据编码</p>
     *
     * @param costCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{costCode}", method = RequestMethod.GET)
    public ApiResult<CostBaseResponse> selectByCode(@PathVariable("costCode") String costCode) {
        ApiResult<CostBaseResponse> apiResult = costRpcService.selectByCode(costCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
